package com.ruoyi.web.controller;

import com.ruoyi.system.domain.Myuser;
import com.ruoyi.system.domain.PictureText;
import com.ruoyi.system.domain.Video;
import com.ruoyi.system.service.IMyuserService;
import com.ruoyi.system.service.IPictureTextService;
import com.ruoyi.system.service.IVideoService;
import com.ruoyi.web.controller.Entity.ListEntity;
import com.ruoyi.web.controller.Entity.WordEvaluation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * 分词器接口
 */
@Controller
@RequestMapping("/system/Ansj")
public class TokenizerController {

    @Autowired
    private IMyuserService myuserService;
    @Autowired
    private IVideoService videoService;
    @Autowired
    private IPictureTextService pictureTextService;

    @PostMapping("/list")
    @ResponseBody
    public ListEntity getList(@RequestParam("Text") String text) {
        WordEvaluation wordEvaluation = new WordEvaluation();//分词器
        Map<String, String> map = wordEvaluation.segMore(text);
        Collection<String> values = map.values();
        HashSet<String> set = new HashSet<>();
        for (String value : values) {
            String[] s = value.split(" ");
            for (String item : s) {
                set.add(item);
            }
        }
        ListEntity list = getListitem(set);
        return list;

    }

    public ListEntity getListitem(HashSet<String> textset) {
        ArrayList<Myuser> userList = new ArrayList<>();
        ArrayList<PictureText> pictureTextArrayList = new ArrayList<>();
        ArrayList<Video> videos = new ArrayList<>();
        for (String value : textset) {
            Myuser myuser = new Myuser();
            myuser.setUserName(value);
            List<Myuser> myusers = myuserService.selectMyuserList(myuser);
            PictureText pictureText = new PictureText();
            pictureText.setTitle(value);
            List<PictureText> pictureTexts = pictureTextService.selectPictureTextList(pictureText);
            Video video = new Video();
            video.setTitle(value);
            List<Video> videoList = videoService.selectVideoList(video);
            userList.addAll(myusers);
            pictureTextArrayList.addAll(pictureTexts);
            videos.addAll(videoList);
        }
        ListEntity listEntity = new ListEntity(userList, pictureTextArrayList, videos);
        return listEntity;
    }

}
